package com.hskn.hss.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.mapper.SysDepartMapper;
import com.hskn.hss.module.permissionRole.entity.SysPermissionRole;
import com.hskn.hss.module.permissionRole.service.ISysPermissionRoleService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class AuthoritySqlUtils {
    @Autowired
    ISysPermissionRoleService sysPermissionRoleService;
    @Autowired
    SysUserDepartMapper sysUserDepartMapper;
    @Autowired
    SysDepartMapper sysDepartMapper;
    public String GetAuthoritySqlByUid(String uid,String createcolumn,String deptcolumn) throws HssException {
        String autioritysql = "";
        List<SysPermissionRole> sysPermissionRoleList =  sysPermissionRoleService.getByUserId(uid);
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        SysUserDepart sysUserDepart = sysUserDepartMapper.selectOne(queryWrapper);
        List idlist = sysDepartMapper.getdDeptIdByParentId(sysUserDepart.getDepid());
        idlist.add(sysUserDepart.getDepid());//添加本部门id到队列

        if(!sysPermissionRoleList.isEmpty()){
            if(sysPermissionRoleList.size()==1) {
                SysPermissionRole sysPermissionRole = sysPermissionRoleList.get(0);
                if(sysPermissionRole.getPermissionid().equals("1")){
                    autioritysql+="and "+createcolumn +" = '"+uid +"'";
                }else if (sysPermissionRole.getPermissionid().equals("2")){
                    autioritysql+="and "+deptcolumn+" = '"+sysUserDepart.getDepid()+"'";
                }else if (sysPermissionRole.getPermissionid().equals("3")){
                    autioritysql+="and "+deptcolumn+" in ('"+ String.join("','", idlist)+"')";
                }else if (sysPermissionRole.getPermissionid().equals("4")){
                    autioritysql+="and "+deptcolumn+" in ('"+ sysPermissionRole.getDeptvalues().replace(",","','")+"')";
                }else {
                    autioritysql+="and 1=1 ";
                }
            }else{
                int comparnum = 0; //对比id值
                List comparlist = new ArrayList(); //所有id记录
                String paramvalus = ""; //参数 拼接

                for ( SysPermissionRole perrole:sysPermissionRoleList) {

                    int permissionid = Integer.parseInt(perrole.getPermissionid());
                    if (!comparlist.contains(permissionid)){
                        comparlist.add(permissionid);
                    }
                    perrole.getPermissionid();
                    if (comparnum>permissionid){
                        comparnum = permissionid;
                    }
                     if(permissionid==4 && !comparlist.contains(4)){
                         paramvalus = perrole.getDeptvalues();
                     }else if (permissionid==4 && comparlist.contains(4)){
                         paramvalus += ","+ perrole.getDeptvalues();
                     }
                }
                if(comparnum==4 && comparlist.contains(2)){
                    paramvalus += ","+ sysUserDepart.getDepid();
                }else if(comparnum==4 && comparlist.contains(3)){
                    paramvalus += ","+ String.join(",", idlist);
                }

                //对比后确定查询结果
                if (comparnum == 1){
                    autioritysql+="and "+createcolumn +" = '"+uid +"'";
                }else if  (comparnum == 2){
                    autioritysql+="and "+deptcolumn+" = '"+sysUserDepart.getDepid()+"'";
                }else if  (comparnum == 3){
                    autioritysql+="and "+deptcolumn+" in ('"+ String.join("','", idlist)+"')";
                }else if(comparnum == 4){
                    autioritysql+="and "+deptcolumn+" in ('"+ paramvalus.replace(",","','")+"')";
                }else {
                    autioritysql+="and 1=1 ";
                }

            }
        }else{
            autioritysql+="and 1=1 ";
        }


        return autioritysql;
    }

    public static void main(String[] args) {
//        List intList = Arrays.asList("1","2","3");
//        String str = String.join("','", intList);
//        System.out.printf(str);

        String s = "1,2,3".replace(",","','");
//        log.info(s);

    }


}
